//
//  UIButton+Extension.swift
//  WeiBo_Private
//
//  Created by Yangperlove on 16/1/18.
//  Copyright © 2016年 Yangperlove. All rights reserved.
//

import UIKit

extension UIButton {
    convenience init(backImageName: String? = nil, stretchable: Bool?, imageName: String? = nil, title: String?, fontSize: CGFloat, titleColor: UIColor?) {
        self.init()
        if backImageName != nil {
            if stretchable == true {
            let backImage = UIImage(named: backImageName!)
            let w = Int(backImage!.size.width * 0.5)
            let h = Int(backImage!.size.height * 0.5)
            setBackgroundImage(backImage?.stretchableImageWithLeftCapWidth(w , topCapHeight: h), forState: .Normal)
                
            let backImageHighLighted = UIImage(named: backImageName! + "_highlighted")
                if backImageHighLighted != nil {
            let w_highLighted = Int(backImageHighLighted!.size.width * 0.5)
            let h_highLighted = Int(backImageHighLighted!.size.height * 0.5)
            setBackgroundImage(backImageHighLighted?.stretchableImageWithLeftCapWidth(w_highLighted , topCapHeight: h_highLighted), forState: .Highlighted)
                }
            } else {
                let backImage = UIImage(named: backImageName!)
                setBackgroundImage(backImage!,forState: .Normal)
                setBackgroundImage(UIImage(named: backImageName! + "_highlighted"),forState: .Highlighted)
            }
        }
        if imageName != nil {
        setImage(UIImage(named: imageName!), forState: .Normal)
        setImage(UIImage(named: imageName! + "_highlighted"), forState: .Highlighted)
        }
        if title != nil {
        setTitle(title, forState: .Normal)
        }
        if titleColor != nil {
        setTitleColor(titleColor, forState: .Normal)
        }

        titleLabel?.font = UIFont.systemFontOfSize(fontSize)

        sizeToFit()
    }

}
